﻿using System;
using System.Collections.Generic;
using System.IO.Packaging;
using System.Text;
using System.Windows;
using System.Xml;
using System.ComponentModel;
using System.Windows.Documents;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows.Controls;

namespace UIPrototyper
{
    public class Screen : DependencyObject, ISelectable, IXML, INotifyPropertyChanged, IDocument
    {
        #region Fields
        private string
            description,
            id,
            name;

        private ScreenType myType;

        private Property property;

        private ControlList<ISelectable>
            controlList;

        private Image image;
        #endregion

        #region Properties
        public ControlList<ISelectable> ControlList
        {
            get
            {
                if (controlList == null)
                {
                    controlList = new ControlList<ISelectable>();
                    controlList.iChanged += new ChangedHandler(controlList_iChanged);
                }

                return controlList;
            }
            set { controlList = value; }
        }

        void controlList_iChanged(IChanged changedObject)
        {
            LaunchChangedEvent(this);
        }

        public ScreenType MyType
        {
            get { return myType; }
            set { myType = value; }
        }

        public Image Image
        {
            get { return image; }
            set { image = value; }
        }

        public string ID
        {
            get { return this.id; }
            set { this.id = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        #endregion

        #region ISelectable Members

        public Property Properties
        {
            get { return property; }
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        #endregion

        #region IXML Members

        public string GenerateXML(Package p)
        {
            StringBuilder xml = new StringBuilder();

            xml.Append("<Screen name=\"" + this.Name + "\"" + " description=\"" + this.Description + "\"" + " id=\"" + this.ID + "\">");

            foreach (FrameworkElement isx in this.ControlList)
            {
                if (isx.GetType() != typeof(SelectionUC))
                {
                    xml.Append(((IXML)isx).GenerateXML(p));
                }
            }

            xml.Append("</Screen>");

            return xml.ToString();
        }

        public void LoadXML(XmlNode pXML)
        {
            ControlFactory cf = new ControlFactory();

            this.Name = pXML.Attributes["name"].Value;
            this.Description = pXML.Attributes["description"].Value;
            this.ID = pXML.Attributes["id"].Value;

            foreach (XmlNode node in pXML.ChildNodes)
            {
                ISelectable fm = cf.CreateControl((ControlType)Enum.Parse(typeof(ControlType), node.Name));
                ((IXML)fm).LoadXML(node);
                this.ControlList.Add(fm);
                this.ControlList.Add(new SelectionUC(fm));
            }
        }

        #endregion

        #region IChanged Members

        public event ChangedHandler iChanged;

        public void LaunchChangedEvent(IChanged pChangedObject)
        {
            if (iChanged != null)
            {
                iChanged(pChangedObject);
            }
        }

        #endregion

        #region ICopy Members

        public ISelectable Copy()
        {
            Screen myCopy = new Screen();

            myCopy.Description = this.description;
            myCopy.ID = this.ID;
            myCopy.Name = this.Name;

            myCopy.ControlList = (ControlList<ISelectable>)(this.ControlList.Copy());

            return myCopy;
        }

        #endregion

        #region Overrides
        public Screen()
        {
            this.property = new ScreenProperties(this);

            ((ScreenProperties)this.property).iChanged += new ChangedHandler(Screen_iChanged);
            //((ScreenProperties)this.property).PropertyChanged += new PropertyChangedEventHandler(Screen_PropertyChanged);
        }

        void Screen_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            PropertyChanged(this, e);
        }

        void Screen_iChanged(IChanged changedObject)
        {
            LaunchChangedEvent(this);
        }

        public override string ToString()
        {
            return this.Name + " {" + this.ID + "}";
        }
        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IDocument Members

        public void Document(ref System.Windows.Documents.FlowDocument pDocument)
        {
            Paragraph title = new Paragraph();
            title.TextAlignment = TextAlignment.Center;

            title.Inlines.Add(new LineBreak());
            title.Inlines.Add(new LineBreak());

            Run titleRun = new Run("Tela: " + this.Name);            
            titleRun.FontWeight = FontWeights.Bold;
            titleRun.FontSize = 22;                   
            title.Inlines.Add(titleRun);

            Paragraph description = new Paragraph();
            title.TextAlignment = TextAlignment.Justify;

            Run descriptionTitle = new Run("Descrição: ");
            descriptionTitle.FontWeight = FontWeights.Bold;
            descriptionTitle.FontSize = 11;
            description.Inlines.Add(descriptionTitle);

            Run descriptionText = new Run(this.Description);
            descriptionText.FontSize = 11;
            description.Inlines.Add(descriptionText);

            Image img = new Image();
            img.Source = this.image.Source;

            BlockUIContainer imageContainer = new BlockUIContainer(img);

            pDocument.Blocks.Add(title);
            pDocument.Blocks.Add(description);
            pDocument.Blocks.Add(imageContainer);

            foreach (FrameworkElement fe in this.ControlList)
            {
                if (fe.GetType() != typeof(SelectionUC))
                {
                    ((IDocument)fe).Document(ref pDocument);
                }
            }
        }

        #endregion
    }
}
